﻿using System.Text;
using System.Security.Cryptography;

namespace MSL.Utility.Security
{
    /// <summary>
    /// 得到随机安全码（哈希加密）。
    /// </summary>
    public class HashEncode
    {
        /// <summary>
        /// 得到随机哈希加密字符串
        /// </summary>
        /// <returns></returns>
        public static string HashEncoding()
        {
            string Security = HashEncoding(GetRandom());
            return Security;
        }
        /// <summary>
        /// 得到一个随机数值
        /// </summary>
        /// <returns></returns>
        private static string GetRandom()
        {
            System.Random Seed = new System.Random();
            string RandomVaule = Seed.Next(1, int.MaxValue).ToString();
            return RandomVaule;
        }
        /// <summary>
        /// 哈希加密一个字符串
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string HashEncoding(string input)
        {
            byte[] Value;
            UnicodeEncoding Code = new UnicodeEncoding();
            byte[] Message = Code.GetBytes(input);
            SHA512Managed Arithmetic = new SHA512Managed();
            Value = Arithmetic.ComputeHash(Message);
            input = "";
            foreach (byte o in Value)
            {
                input += (int)o + "O";
            }
            return input;
        }
    }
}
